PROJECT_DIR := $(shell pwd)
TCP_PATH := $(PROJECT_DIR)/route_connect/tcp_link

INCLUDE_PATH := -I$(TCP_PATH)/tcp_client/ \
                -I$(TCP_PATH)/tcp_service/

SOURCE_PATH := $(TCP_PATH)/tcp_client/tcp_client_sdk.cpp \
               $(TCP_PATH)/tcp_service/tcp_service_sdk.cpp

OBJ := main
SRC := main_enc_dec.cpp

CPPFLAGS := -std=c++11 -pthread

CXX :=
ARCH :=
CROSS_COMPILE :=

ifeq ($(ARCH), arm)
    CXX = $(CROSS_COMPILE)g++
else
    CXX = g++
endif

.PHONY: OUTPUT print
    
all: print $(OBJ) OUTPUT 

print:
	@echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
	@echo "PROJECT_DIR: $(PROJECT_DIR)"
	@echo "TCP_PATH: $(TCP_PATH)"
	@echo "INCLUDE_PATH: $(INCLUDE_PATH)"
	@echo "SOURCE_PATH: $(SOURCE_PATH)"
	@echo "OBJ: $(OBJ)"
	@echo "SRC: $(SRC)"
	@echo "CPPFLAGS: $(CPPFLAGS)"
	@echo "ARCH: $(ARCH)"
	@echo "CXX: $(CXX)"
	@echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"

%.o: %.cpp
	$(CXX) -c $< -o $@ $(INCLUDE_PATH) $(CPPFLAGS)

$(OBJ): $(SRC) $(SOURCE_PATH:.cpp=.o)
	$(CXX) $^ -o $@ $(INCLUDE_PATH) $(CPPFLAGS)

tmp:=$(patsubst %.cpp,%.o,$(SOURCE_PATH))

OUTPUT:
	@if [ -e "$(PROJECT_DIR)/out" ]; then \
		echo "目录 $(PROJECT_DIR)/out 已存在"; \
	else \
		echo "目录 $(PROJECT_DIR)/out 不存在，正在创建..."; \
		mkdir -p "$(PROJECT_DIR)/out/obj"; \
	fi
	@for file in $(tmp); do \
		mv "$$file" "$(PROJECT_DIR)/out/obj/"; \
	done
clean:
	rm -f $(OBJ) *.o $(PROJECT_DIR)/out/obj/*.o